#!/usr/bin/perl
# Version 0.2.1

$testcase = "flooding";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 3) {
        print("Usage: $testcase.pl dump_file1 ... dump_file3 log_file");
        exit 1;
}

$dumpfile[0] = $ARGV[0];
$dumpfile[1] = $ARGV[1];
$dumpfile[2] = $ARGV[2];
$logfile = $ARGV[3];

#------------------------------------------------------------------------------

@local_verdict = ( 3, 3, 3 );
@a_rut_tr1 = ( "0.0.0.74", "0.0.0.42", "0.0.0.10");
@a_rut_n2 = ( "0.0.0.69", "0.0.0.37", "0.0.0.5");
@a_rut_n3 = ( "0.0.0.65", "0.0.0.33", "0.0.0.1");

for($i = 0; $i < 3; $i++) {
	$success = 1;
	$state = 0;
	open(DUMPFILE, $dumpfile[$i]) or $success = 0;

	if($success) {
		while(<DUMPFILE>) {
			# wait for the databases to synchronize. after 50s there should be a new LSA flooded to the RUT
			if($state eq 0) {
				if(/(.*) (.*) > (.*): OSPFv2-ls_upd/) {
					if( $3 eq $a_rut_tr1[$i]) {
						# now look at the time
						@ti = split(/\./, $1);
						@ts = split(/:/, $ti[0]);
			
						$secs = $ts[2] + ($ts[1] + $ts[0] * 60) * 60; # secs contains the sim time in seconds
				
						if ($secs > 50) {
							$state = 1;
						}
					}
				}
			}
			if($state > 0) {
				if(/(.*) (.*) > (.*): OSPFv2-ls_upd/) {
					
					if( $2 eq $a_rut_n2[$i] ) {
						# look if the RUT correctly sends an update out of its interface to network 2
						$state = $state | 2;
					} elsif ($2 eq $a_rut_n3[$i] ) {
						# and check, that it doesn't send an update out of its interface to network 3
						$state = $state | 4;
					}
				}
			}	
		}
	
		# in the end, the state must be equal to 3
		if($state eq 3) {
			$local_verdict[$i] = 1;
		} else {
			$local_verdict[$i] = 2;
		}
	}
	
	if ($local_verdict[$i] eq 1) {
		print("RUT properly floods the LS update. Test PASSED.\n");
	} elsif ($local_verdict[$i] eq 2) {
		print("RUT doesn't flood the LS update correctly ($state). Test FAILED.\n");
	} else {
		print("Cannot evaluate test results. Test INCONCLUSIVE.\n");
	}
}


$verdict = $local_verdict[0];
for($i = 1; $i < 3; $i++) {
	if ($local_verdict[$i] ne $verdict) {
		if($local_verdict[$i] eq 2) {
			$verdict = 2;
		} elsif ($local_verdict[$i] eq 3) {
			if($verdict ne 2) {
				$verdict = 3;
			}
		}	
	}
}

#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");